package com.ruoyi.common.utils;

import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * id生成工具类
 *
 * @author shushuang
 */
public class CreateIdUtils {


    private static final Object lockone = new Object();

    private static final Object locktwo = new Object();

    /**
     * This int is the sequence number ,the default value is 0.
     */
    private static int seq = 0;

    /**
     * The FieldPosition.
     */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /**
     * This Format for format the number to special format.
     */
    private final static NumberFormat numberFormat = new DecimalFormat("0000");

    /**
     * This Format for format the data to special format.
     */
    private final static Format dateFormat16 = new SimpleDateFormat("YYMMddHHmmss");
    private final static Format dateFormat18 = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static Format dateFormat19 = new SimpleDateFormat("YYMMddHHmmssSSS");



    /**
     * 生成原生uuid,带有--
     * @return String
     */
    public static String getGenerateUUID(){
        return UUID.randomUUID().toString();
    }

    /**
     * 生成去-的uuid
     * @return
     */
    public static String getReGenerateUUID(){
        return getGenerateUUID().replace("-","");
    }

    /**
     * 当前日期生成id+四位随机数据new SimpleDateFormat(YYYYMMDDHHMMSS).format(new Date())
     * @return String
     */
    public static String getDateId(){
        return dateFormat18.format(new Date()) + String.valueOf(new Random().nextInt(9000) + 1000);
    }

    /**
     * 时间格式生成16位序列ID （注：1秒内最多调用一万次，否则会出现重复）
     * 性能十万次耗时约320ms
     *
     * @return Long
     */
    public static Long get16UUID() {
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        synchronized(lockone){
            return getFormatNum(dateFormat16,sb,rightNow);
        }
    }

    /**
     * 时间格式生成19位序列ID （注：1毫秒内最多调用一万次，否则会出现重复）
     * 注：到2092年时该数字会超出long的范围，会抛异常
     * @return Long
     */
    public static Long get19UUID(){
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        synchronized(locktwo){
            return getFormatNum(dateFormat19,sb,rightNow);
        }
    }

    private static Long getFormatNum(Format dateFormat, StringBuffer sb, Calendar rightNow){
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        numberFormat.format(seq, sb, HELPER_POSITION);
        if (seq == 9999) {
            seq = 0;
        } else {
            seq++;
        }
        return Long.parseLong(sb.toString());
    }

    /**
     * 根据长度生成随机字符
     * @param length
     * @return
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }



    public static void main(String[] args) {
        System.out.printf(getGenerateUUID());
    }










}
